package javax.realtime;

public class PriorityScheduler extends Scheduler {

	public static final int MAX_PRIORITY = NativeHelper.getMaxRTPriority();
	public static final int MIN_PRIORITY = NativeHelper.getMinRTPriority();
	private static PriorityScheduler instance = new PriorityScheduler();
	
	protected PriorityScheduler(){}

	@Override
	protected boolean addToFeasibility(Schedulable schedulable) {
		if(schedulable == null) throw new IllegalArgumentException();
		if(schedulable.getScheduler() != this) throw new IllegalArgumentException();
		//return schedulable.addToFeasibility();
		return true;
	}
	
	public void fireSchedulable(Schedulable schedulable)
	{
		/*
		if(schedulable instanceof RealtimeThread) ((RealtimeThread)schedulable).start();
		else
		{
			RealtimeThread thread = new RealtimeThread(schedulable.getSchedulingParameters(), schedulable.getReleaseParameters(), schedulable.getMemoryParameters(), null, schedulable.getProcessingGroupParameters(), schedulable);
			thread.start();
		}*/
		throw new UnsupportedOperationException();
	}
	
	public int getMaxPriority()
	{
		return MAX_PRIORITY;
	}
	
	public static int getMaxPriority(Thread thread)
	{
		if(thread == null) return MAX_PRIORITY;
		else if(thread instanceof Schedulable)
		{
			if(((Schedulable)thread).getScheduler() instanceof PriorityScheduler) return ((PriorityScheduler)((Schedulable)thread).getScheduler()).getMaxPriority();
		}
		return Thread.MAX_PRIORITY;
	}
	
	public int getMinPriority()
	{
		return MIN_PRIORITY;
	}
	
	public static int getMinPriority(Thread thread)
	{
		if(thread == null) return MIN_PRIORITY;
		else if(thread instanceof Schedulable)
		{
			if(((Schedulable)thread).getScheduler() instanceof PriorityScheduler) return ((PriorityScheduler)((Schedulable)thread).getScheduler()).getMinPriority();
		}
		return Thread.MIN_PRIORITY;
	}
	
	public int getNormPriority()
	{
		return (MAX_PRIORITY - MIN_PRIORITY) / 3 + MIN_PRIORITY;
	}
	
	public static int getNormPriority(Thread thread)
	{
		if(thread == null) return instance.getNormPriority();
		else if(thread instanceof Schedulable)
		{
			if(((Schedulable)thread).getScheduler() instanceof PriorityScheduler) return ((PriorityScheduler)((Schedulable)thread).getScheduler()).getNormPriority();
			else throw new IllegalArgumentException();
		}
		return Thread.NORM_PRIORITY;
	}
	
	public String getPolicyName()
	{
		return "Fixed Priority";
	}
	
	public static PriorityScheduler instance()
	{
		return instance;
	}
	
	public boolean isFeasible()
	{
		return true;
	}
	
	protected boolean removeFromFeasibility(Schedulable schedulable)
	{
		if(schedulable == null) throw new IllegalArgumentException();
		if(schedulable.getScheduler() != this) return false;
		//return schedulable.removeFromFeasibility();
		return true;
	}
	
	public boolean setIfFeasible(Schedulable schedulable, ReleaseParameters release, MemoryParameters memory)
	{
		//TODO: stub, PGP
		//return setIfFeasible(schedulable, release, memory, schedulable.getProcessingGroupParameters());
		return true;
	}
	
	public boolean setIfFeasible(Schedulable schedulable, ReleaseParameters release, MemoryParameters memory, ProcessingGroupParameters group)
	{
		return setIfFeasible(schedulable, schedulable.getSchedulingParameters(), release, memory, group);
	}
	
	public boolean setIfFeasible(Schedulable schedulable, SchedulingParameters scheduling, ReleaseParameters release, MemoryParameters memory, ProcessingGroupParameters group)
	{
		//TODO: PGP, MP
		if(schedulable == null) throw new IllegalArgumentException();
		if(schedulable.getScheduler() != this) throw new IllegalArgumentException();
		
		if(scheduling != null) schedulable.setSchedulingParameters(scheduling);
		if(release != null) schedulable.setReleaseParameters(release);
		//if(memory != null) schedulable.setMemoryParameters(memory);
		//if(group != null) schedulable.setProcessingGroupParameters(group);
		
		return true;
	}

/*
	@Override
	public MemoryParameters getDefaultMemoryParameters() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ProcessingGroupParameters getDefaultProcessingGroupParameters() {
		// TODO Auto-generated method stub
		return null;
	}
*/
	@Override
	public ReleaseParameters getDefaultReleaseParameters() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SchedulingParameters getDefaultSchedulingParameters() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean setIfFeasible(Schedulable schedulable,
			ReleaseParameters release) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean setIfFeasible(Schedulable schedulable,
			SchedulingParameters scheduling, ReleaseParameters release) {
		// TODO Auto-generated method stub
		return false;
	}
}
